package com.hemerasolutions.v2trainer.dao;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Join;
import javax.persistence.criteria.JoinType;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.stereotype.Repository;

import com.hemerasolutions.v2trainer.dao.interfaces.AcademyDaoInterface;
import com.hemerasolutions.v2trainer.model.users.Academy;
import com.hemerasolutions.v2trainer.model.users.User;
import com.hemerasolutions.v2trainer.model.users.User_;


@Repository(value = "academyDao")
public class AcademyDao extends BaseDao<Academy> implements AcademyDaoInterface{
	private static final long serialVersionUID = 1L;

	@Override
	public List<Academy> search(Map<String, Object> fields) {

		List<Predicate> predicateList = new ArrayList<Predicate>();

		CriteriaBuilder cb = this.getEntityManager().getCriteriaBuilder();

		CriteriaQuery<Academy> cq  = cb.createQuery(Academy.class);
		
		Root<Academy> academy = cq.from(Academy.class);

		Predicate pred;

		Join<Academy,User> junction = academy.join("user",JoinType.LEFT); 
		
		
	    for (Map.Entry<String,Object> line : fields.entrySet()) {
	    	
	    	if( line.getKey().toString() == "userName" 	 ||
		   		line.getKey().toString() == "email" ){
	    		
	    		pred = cb.like(cb.upper(junction.<String>get(line.getKey().toString())), 
	    								"%"+line.getValue().toString().toUpperCase()+"%");
	            predicateList.add(pred);    		
	    	}

	    	
	    	
	    	if( line.getKey().toString() == "name" 	 			||
	    			line.getKey().toString() == "owner" 		||
	    			line.getKey().toString() == "state" 		||
					line.getKey().toString() == "city" 	 		||
					line.getKey().toString() == "address" 		||
					line.getKey().toString() == "postalCode" 	||
					line.getKey().toString() == "cnpj" 	 ){
	    		
	    		pred = cb.like(cb.upper(academy.<String>get(line.getKey().toString())), 
        								"%"+line.getValue().toString().toUpperCase()+"%");
                predicateList.add(pred);    		
        	}	    	
	    	
	    	
	    	if( line.getKey().toString() == "begCreatedAt" ){ 
	    		pred = cb.greaterThanOrEqualTo(junction.<Date>get(User_.createdAt), (Date)line.getValue());
	    		predicateList.add(pred);    		
	    	}
	    	
	    	if( line.getKey().toString() == "endCreatedAt" ){ 
	    		pred = cb.lessThanOrEqualTo(junction.<Date>get(User_.createdAt), this.endDayDateHour((Date)line.getValue()));
	    		predicateList.add(pred);    		
	    	}
	    	
	    	if( line.getKey().toString() == "begChangedAt" ){ 
	    		pred = cb.greaterThanOrEqualTo(junction.<Date>get(User_.lastChange), (Date)line.getValue());
	    		predicateList.add(pred);    		
	    	}

	    	if( line.getKey().toString() == "endChangedAt" ){ 
	    		pred = cb.lessThanOrEqualTo(junction.<Date>get(User_.lastChange), this.endDayDateHour((Date)line.getValue()));
	    		predicateList.add(pred);    		
	    	}

	    	if( line.getKey().toString() == "begExpireOn" ){ 
	    		pred = cb.greaterThanOrEqualTo(junction.<Date>get(User_.expireOn), (Date)line.getValue());
	    		predicateList.add(pred);    		
	    	}

	    	if( line.getKey().toString() == "endExpireOn" ){ 
	    		pred = cb.lessThanOrEqualTo(junction.<Date>get(User_.expireOn), this.endDayDateHour((Date)line.getValue()));
	    		predicateList.add(pred);    		
	    	}
	    	
	    	
	   		if( line.getKey().toString() == "active"){ 
	    		pred = cb.equal(junction.<Boolean>get(User_.active), (Boolean)line.getValue());
	    		predicateList.add(pred);    		
	       	}
	    }
	    
	    Predicate[] predicates = new Predicate[predicateList.size()];
	    predicateList.toArray(predicates);
	    cq.where(predicates);
	 
	    return super.getEntityManager().createQuery(cq).getResultList();			
		
		
	}
}
